//
//  JYPopoverAnimation.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/2.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

class JYPopoverAnimation: NSObject {
    // MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRectZero
    
    
    // MARK:- 属性
    private var isPresented : Bool = false
    private var presentedBack : ((presented : Bool) ->())?
    
    // MARK:- 自定义构造函数
    init(presentedBack : (presented : Bool) -> ()) {
        self.presentedBack = presentedBack
    }
}

// MARK:- 转场代理
extension JYPopoverAnimation : UIViewControllerTransitioningDelegate {
    //目的:改变弹出的view的尺寸
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentVc = JYPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentVc.presentedFrame = presentedFrame
        
        return presentVc
    }
    
    //目的：自定义弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        presentedBack!(presented : isPresented)
        return self
    }
    
    //目的：自定义消失动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        presentedBack!(presented : isPresented)
        return self
    }
}


// MARK:- 弹出和消失动画的代理方法
extension JYPopoverAnimation : UIViewControllerAnimatedTransitioning {
    //动画执行时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    //获取转场上下文：可以通过上下文获取弹出的view和消失的view
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext)
    }
    
    //自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        //获取弹出的view
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //将view添加到containView中
        transitionContext.containerView()?.addSubview(presentedView)
        
        //执行动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0)
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            presentedView.transform = CGAffineTransformIdentity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
    }
    
    //自定义消失动画
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {
        //获取消失的view
        let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        //执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            dismissedView?.transform = CGAffineTransformMakeScale(1.0, 0.01)
        }) { (_) in
            dismissedView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}